package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//统一异常处理
@RestControllerAdvice
@Slf4j

public class ExceptionCatch {


    //捕获所有异常处理
    @ExceptionHandler(value = Exception.class)
    public ResponseResult exception(Exception exception) {
        exception.printStackTrace();
        //记录打印日志
        log.error("catch exception:{}", exception.getMessage());
        //返回统一异常处理结果
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @ExceptionHandler(CustomException.class)
    public ResponseResult customException(Exception e){
        // 异常是否属于 自定义异常类型
        if(e instanceof CustomException){
            return ((CustomException) e).getResponseResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}